<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>工程首页</h1>
    <a href="/reg.html">注册</a>
    <a href="/login.html">登录</a>
    <h2>{{info}}</h2>
    <!--如果登陆过显示发布微博-->
    <div v-if="info!='请先登录!'">
        <a href="/insert.html">发布微博</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3>{{weibo.nick}}说:{{weibo.content}}</h3>
        <span>发布日期:{{weibo.created}}</span>
        <!--判断当前登录的用户id 等于微博的userId 则显示删除-->
        <a v-if="user.id==weibo.userId" href="javascript:void(0)"
           @click="del(weibo.id)">删除</a>
    </div>
</div>
<!--引入Vue框架-->
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el: "div",
        data: {
            user: {},
            info:"",
            arr:[]
        },
        created: function () {
            axios.get("/select").then(function (response) {
                v.arr = response.data;
            })

            //发请求得到当前的登录信息
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
                console.log("服务器返回的内容:"+response.data);
                if (response.data==""){   //代码还未登录
                    v.info="请先登录!";
                }else {  // 代表登陆过
                    v.info="欢迎"+v.user.nick;
                }
            })
        },
        methods: {
            logout(){
                axios.get("/logout").then(function () {
                    v.info="请先登录!";
                    // 去掉当前登录的用户对象，为让删除不显示
                    v.user="";
                })
            },
            del(id){
                if(confirm("您确认删除这条微博吗?")){
                    axios.get("/delete?id="+id).then(function () {
                        location.reload(); // 刷新页面
                    })
                }
            }
        }
    })
</script>
</body>
</html>